package cn.gene.timeyan.article.webapi.service.impl;

import cn.gene.timeyan.article.service.ArticleService;
import cn.gene.timeyan.article.service.LikeService;
import cn.gene.timeyan.article.service.Repository;
import cn.gene.timeyan.article.webapi.util.LikedStatusEnum;
import cn.gene.timeyan.pojo.article.model.Article;
import cn.gene.timeyan.pojo.article.model.ArticleVO;
import cn.gene.timeyan.pojo.article.model.Like;
import cn.gene.timeyan.article.webapi.mapper.LikeMapper;
import cn.gene.timeyan.pojo.article.model.LikeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LWG
 * @since 2022-07-25
 */
@Service
public class LikeServiceImpl implements LikeService {
   /* @Autowired(required = false)
    private Repository repository;
    @Autowired(required = false)
    private LikeMapper likeMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private ArticleService articleService;

    @Override
    public LikeVO save(LikeVO likeVO) {
        return likeMapper.save(likeVO);
    }

    @Override
    public List<LikeVO> saveAll(List list) {
        return likeMapper.saveAll(list);
    }

    @Override
    public Page<LikeVO> getLikedListByLikedUserId(Long  byUserId, Pageable pageable) {
        return likeMapper.findLikedListByLikedUserIdAndStatus(byUserId, LikedStatusEnum.LIKE.getCode(), pageable);
    }

    @Override
    public Page<LikeVO> getLikedListByLikedPostId(Long  likeId, Pageable pageable) {
        return likeMapper.findLikedListByLikedPostIdAndStatus(likeId, LikedStatusEnum.LIKE.getCode(), pageable);
    }

    @Override
    public LikeVO getByLikedUserIdAndLikedPostId(Long  likeId, Long  byUserId) {
        return likeMapper.findByLikedUserIdAndLikedPostId(likeId, byUserId);
    }

    @Override
    public void transLikedFromRedis2DB() {
        List<LikeVO> list = repository.getLikedDataFromRedis();
        for (LikeVO likeVO : list) {
            LikeVO ul = getByLikedUserIdAndLikedPostId(likeVO.getLikeId(), likeVO.getByUserId());
            if (ul == null) {
                //没有记录，直接存入
                save(likeVO);
            } else {
//有记录，需要更新
                ul.setLikeState(likeVO.getLikeState());
                save(ul);
            }

        }
    }

    @Override
    public void transLikedCountFromRedis2DB() {
        List<ArticleVO> list = repository.getLikedCountFromRedis();

        for (ArticleVO vo : list) {

            Article article = articleService.selectByUserId(vo.getUserId());

//点赞数量属于无关紧要的操作，出错无需抛异常

            if (article != null) {

                Integer likeNum = article.getArticleLike() + vo.getArticleLike();

                Long userId = article.getUserId();
                Long articleId=article.getArticleId();
//更新点赞数量
                articleService.updateInfo(articleId, userId, likeNum);

            }
        }
    }*/
}
